﻿#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>
#include <QtDebug>

#include "config.h"
#include "logindlg.h"
#include "db.h"
#include<qtextcodec.h>
#pragma execution_character_set("utf-8")

LoginDlg::LoginDlg(bool auth, QWidget* parent)
    : QDialog(parent)
{

    QTextCodec* code=QTextCodec::codecForName("utf-8");

    QTextCodec::setCodecForLocale(code);
    if (auth) {
        this->setWindowTitle(tr("用户授权 -- 当前用户： ") + GYSCADAConfig::getInstance()->getStafferName());
    } else {
        this->setWindowTitle(tr("用户登录"));
    }
    this->resize(290, 160);

    /* Set User Name */
    labelUserName.setParent(this);
    labelUserName.setText(tr("用户名："));
    labelUserName.move(30, 33);

    editUserName.setParent(this);
    editUserName.resize(180, 20);
    editUserName.move(80, 30);
    if (auth) {
        editUserName.setText(GYSCADAConfig::getInstance()->getUserName());
        editUserName.setEnabled(false);
    }

    /* Set Password */
    labelPassword.setParent(this);
    labelPassword.setText(tr("密码："));
    labelPassword.move(30, 73);

    editPassword.setParent(this);
    editPassword.resize(180, 20);
    editPassword.move(80, 70);
    editPassword.setEchoMode(QLineEdit::Password);

    /* Set login button */
    if (auth) {
        btnLogin.setText(tr("授权"));
        connect(&btnLogin, SIGNAL(released()), this, SLOT(authorize()));
    } else {
        btnLogin.setText(tr("登录"));
        connect(&btnLogin, SIGNAL(released()), this, SLOT(login()));
    }
    btnLogin.setParent(this);
    btnLogin.move(186, 110);

    /* Set exit button */
    if (auth) {
        btnExit.setText(tr("切换用户"));
        connect(&btnExit, SIGNAL(released()), this, SLOT(switchUser()));
    } else {
        btnExit.setText(tr("退出"));
        connect(&btnExit, SIGNAL(released()), this, SLOT(exit()));
    }
    btnExit.setParent(this);
    btnExit.move(30, 110);
}

LoginDlg::~LoginDlg()
{
}

void LoginDlg::login()
{
    QString userName = editUserName.text();
    QString password = editPassword.text();
    
    if (GYSCADADB::getInstance()->verify(userName, password)) {
        GYSCADAConfig::getInstance()->setStafferName(GYSCADADB::getInstance()->getStafferName(userName, password));
        GYSCADAConfig::getInstance()->setUserId(GYSCADADB::getInstance()->getUserId(userName, password));
        GYSCADAConfig::getInstance()->setUserName(userName);

        this->accept();
    } else {
        QMessageBox::critical(NULL, tr("错误"), tr("登录失败，用户名或者密码不正确！"));
    }
}

void LoginDlg::authorize()
{
    QString userName = editUserName.text();
    QString password = editPassword.text();
    
    if (GYSCADADB::getInstance()->verify(userName, password)) {
        this->accept();
    } else {
        QMessageBox::critical(NULL, tr("错误"), tr("授权失败，密码不正确！"));
    }
}

void LoginDlg::exit()
{
    this->close();
}

void LoginDlg::switchUser()
{
    this->setWindowTitle(tr("用户登录"));

    editUserName.setEnabled(true);
    editUserName.clear();
    editUserName.setFocus();

    editPassword.clear();

    btnLogin.setText(tr("登录"));
    btnLogin.disconnect();
    connect(&btnLogin, SIGNAL(released()), this, SLOT(login()));

    btnExit.setText(tr("退出"));
    btnExit.disconnect();
    connect(&btnExit, SIGNAL(released()), this, SLOT(exit()));
}
